package com.link.base.base.fund.service;

import com.link.base.base.cmqres.model.CmqRes;
import com.link.base.base.cmqres.service.CmqResService;
import com.link.base.base.fund.dao.mybatis.mapper.DealerAccountMapper;
import com.link.base.base.fund.model.DealerAccount;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.mq.util.MqUtil;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  unknwon
 * @date:   2018/11/12 10:12
 * @version 1.0
 */

@Service
public class DealerAccountServiceImpl extends BasicServiceImpl<DealerAccount> implements DealerAccountService {

    @Resource
    private DealerAccountMapper dealerAccountMapper;

    @Resource
    private CmqResService cmqResService;

    @Override
    public BasicMapper<DealerAccount> getBasicMapper() {
        return dealerAccountMapper;
    }

    @Override
    public void beforUpsert(DealerAccount entity) throws Exception {
        Long accntId = entity.getAccntId();
        String type = entity.getType();
        if (null == accntId) {
            throw new ServiceException("PUBLIC-026");
        }
        if (StringUtils.isBlank(type)) {
            throw new ServiceException("FUND-016");
        }
        DealerAccount dealerAccountParam = new DealerAccount();
        dealerAccountParam.setAccntId(accntId);
        dealerAccountParam.setType(type);
        dealerAccountParam.setPageFlag(false);
        List<DealerAccount> dealerAccountList = queryByExamplePage(dealerAccountParam);
        if (dealerAccountList.size() == 1 && !dealerAccountList.get(0).getId().equals(entity.getId())) {
            throw new ServiceException("FUND-017");
        }
        if (dealerAccountList.size() > 1) {
            throw new ServiceException("FUND-017");
        }
        super.beforUpsert(entity);
    }

    @Override
    public List<DealerAccount> queryByPeriod(DealerAccount dealerAccount) {
        //if(dealearAccount.)
        return dealerAccountMapper.queryByPeriod(dealerAccount);
    }

    /**
     * @author huanglongjian
     * @param dealerAccount
     * @return
     * @throws Exception
     * @ 删除指定的用户的资金账户
     */
    @Override
    public List<DealerAccount> queryByAccountId(DealerAccount dealerAccount) throws Exception {
        List<DealerAccount> list = dealerAccountMapper.queryByAccountId(dealerAccount);
        return list;
    }

    @Override
    public void afterUpdate(DealerAccount dealerAccount) throws Exception {
        CmqRes cmqRes = new CmqRes();
        List<CmqRes> cmqResList = cmqResService.queryAll(cmqRes);
        if (cmqResList.size() > 0) {
            try {
                Long accntId = dealerAccount.getAccntId();
                if (null != accntId) {
                    String message1 = "AccountSyncl;{\"action\":\"Update\",\"objectId\":\"";
                    String message2 = "\"}";
                    String message = message1 + accntId + message2;
                    String topicName = cmqResList.get(0).getQueueName();
                    MqUtil.sendCmqTopic(topicName, message);
                }
            } catch (Exception e) {
                LCLogger.withException(e);
            }
        }
    }

    @Override
    public void afterInsert(DealerAccount dealerAccount) throws Exception {
        CmqRes cmqRes = new CmqRes();
        List<CmqRes> cmqResList = cmqResService.queryAll(cmqRes);
        if (cmqResList.size() > 0) {
            try {
                Long accntId = dealerAccount.getAccntId();
                if (null != accntId) {
                    String message1 = "AccountSyncl;{\"action\":\"Update\",\"objectId\":\"";
                    String message2 = "\"}";
                    String message = message1 + accntId + message2;
                    String topicName = cmqResList.get(0).getQueueName();
                    MqUtil.sendCmqTopic(topicName, message);
                }
            } catch (Exception e) {
                LCLogger.withException(e);
            }
        }
    }

    /**
     * @param dealerAccount 资金账户对象
     * @return 资金账户列表
     * @throws Exception 报错
     */
    @Override
    public DealerAccount queryItemSumByTypeAndAccntId(DealerAccount dealerAccount) throws Exception {
        String type = dealerAccount.getType();
        Long accntId = dealerAccount.getAccntId();

        if (StringUtils.isNull(type)) {
            throw new ServiceException("FUND-016");
        }
        if (null == accntId) {
            throw new ServiceException("PUBLIC-026");
        }
        List<DealerAccount> dealerAccountList = dealerAccountMapper.queryItemSumByTypeAndAccntId(dealerAccount);
        if (dealerAccountList == null) {
            throw new ServiceException("PUBLIC-052");
        }
        if (dealerAccountList.size() > 1) {
            throw new ServiceException("PUBLIC-053");
        }
        if (dealerAccountList.size() < 1) {
            throw new ServiceException("PUBLIC-052");
        }
        DealerAccount dealerAccountRet = null;
        dealerAccountRet = dealerAccountList.get(0);
        return dealerAccountRet;
    }

    /**
     * 更新一条数据
     *
     * @param entity 资金账户对象
     */
    @Override
    public void updateAmount(DealerAccount entity) throws Exception {
        dealerAccountMapper.updateAmount(entity);
    }
}
